# -*- coding: utf-8 -*-
import xlrd
import xlwt
from datetime import datetime
from xlrd import xldate_as_tuple

# 从excel中读取
def creat_excel_from_excel():
    book = xlwt.Workbook()
    ExcelFile = xlrd.open_workbook(r'C:\Users\Administrator\Desktop\test1.xls')
    sheet_name_list = ExcelFile.sheet_names()
    for sheet_name in sheet_name_list:
        newSheet = book.add_sheet(sheet_name)
        oldSheet = ExcelFile.sheet_by_name(sheet_name)
        sheetRows = oldSheet.nrows
        sheetCols = oldSheet.ncols
        i = 0
        while i < sheetRows:
            j = 0
            rows = oldSheet.row_values(i)
            while j < sheetCols:
                cell = oldSheet.cell(i, j)
                print(cell)
                cvalue = cell.value
                ctype = cell.ctype
                if ctype == 2 and (cvalue % 1 == 0):
                    cvalue = cvalue
                elif ctype == 3:
                    # 转成datetime对象
                    date = datetime(*xldate_as_tuple(cvalue, 0))
                    cvalue = date.strftime('%Y/%d/%m %H:%M:%S')
                elif ctype == 4:
                    cvalue = True if cvalue == 1 else False
                j += 1
                newSheet.write(i, j, cvalue)
                print(cvalue)
            i += 1
    book.save(r'C:\Users\Administrator\Desktop\firstsheet.xls')

if __name__ =='__main__':
    creat_excel_from_excel()